﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace DevDemo
{
    public partial class TableLayOutDemo3 : Form
    {
        public TableLayOutDemo3()
        {
            InitializeComponent();
        }

        TableLayoutPanel tb = new TableLayoutPanel();

        public string SelectControlTagID = "";

        string[] str_arr = { "1", "2", "3", "4", "5" };

        private void TableLayOutDemo3_Load(object sender, EventArgs e)
        {
            RELOAD();
        }

        /// <summary>
        /// 初始化加载TableLayOutControl
        /// </summary>
        public void RELOAD()
        {
            TableLayoutPanel table = new TableLayoutPanel();

            this.panel1.Controls.Clear();

            // 默认添加一行数据
            table.Dock = DockStyle.Top;

            panel1.Controls.Add(table);

            table.ColumnCount = 1;

            table.Height = table.RowCount * 80;

            int step = 100 / table.ColumnCount;

            table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, step));

            for (int ii = 0; ii < table.RowCount; ii++)
            {
                table.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40));
            }

            AddTableRows(table, str_arr);
        }

        /// <summary>
        /// 加载行控件方法
        /// </summary>
        /// <param name="str_arr"></param>
        public void AddTableRows(TableLayoutPanel table, string[] str_arr)
        {
            ArrayList list = new ArrayList(str_arr);

            int rows = 0;

            foreach (var item in list)
            {
                //动态添加一行
                table.RowCount++;
                //设置高度
                table.Height = table.RowCount * 65;
                //行高
                table.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 60));
                //设置cell样式，
                table.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;

                //int ii = table.RowCount - 1;

                GroupBox gb = new GroupBox();

                gb.Text = item + "Group";

                gb.Height = 50;

                gb.Width = 300;

                gb.Tag = rows;

                gb.MouseDown += new MouseEventHandler(gb_MouseDown);

                Button btn = new Button();

                btn.Text = "Del";

                btn.Tag = rows;

                btn.Click += new EventHandler(btn_Click);

                btn.Location = GetLocal(20, 20);

                gb.Controls.Add(btn);

                table.Controls.Add(gb, 0, rows);

                // 重新计算高度，否则最后一行偏大
                table.Height = table.RowCount * 80;

                rows++;
            }

            tb = table;
        }

        /// <summary>
        /// 删除控件事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_Click(object sender, EventArgs e)
        {
            var obj = (Button)sender;

            RemoveControls(this.panel1, tb, obj.Tag.ToString());
        }

        /// <summary>
        /// 删除控件方法
        /// </summary>
        /// <param name="items"></param>
        /// <param name="tagId"></param>
        public void RemoveControls(Control items, TableLayoutPanel table, string tagId)
        {
            foreach (var item in items.Controls)
            {
                if (item.GetType().ToString().ToUpper() == "SYSTEM.WINDOWS.FORMS.GROUPBOX")
                {
                    var b = (GroupBox)item;

                    if (b.Tag.ToString() == tagId)
                    {
                        int rowIndex = table.GetRow(b);

                        table.Controls.RemoveAt(rowIndex);

                        // 移动,当前行row的下行往上移动
                        for (int k = rowIndex; k < table.RowCount - 1; k++)
                        {
                            Control ctlNext = table.GetControlFromPosition(0, k + 1);

                            table.SetCellPosition(ctlNext, new TableLayoutPanelCellPosition(0, k));
                        }

                        table.RowStyles.RemoveAt(table.RowCount - 1);

                        table.RowCount = table.RowCount - 1;

                        break;
                    }
                }
                else
                {
                    RemoveControls((Control)item, table, tagId);
                }
            }

            // 重新计算高度，否则最后一行偏大
            table.Height = table.RowCount * 80;
        }

        /// <summary>
        /// 返回定位
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public Point GetLocal(int x, int y)
        {
            Point p = new Point();

            p.X = x;

            p.Y = y;

            return p;
        }

        /// <summary>
        /// 重置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            RELOAD();
        }
        

        /// <summary>
        /// 右键删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(SelectControlTagID))
            {
                RemoveControls(this.panel1, tb, SelectControlTagID);
            }
        }

        /// <summary>
        /// 右键点击获取值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gb_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                var obj = (GroupBox)sender;

                SelectControlTagID = obj.Tag.ToString();
            }
        }

        /// <summary>
        /// Add
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            ArrayList ar = new ArrayList(str_arr);

            ar.Add("6");

            string[] arrString = (string[])ar.ToArray(typeof(string));

            str_arr = arrString;

            this.panel1.Controls.Clear();

            RELOAD();
        }

        /// <summary>
        /// DelAll
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            //table.Controls.Clear();

            //table.ColumnStyles.Clear();

            //table.RowStyles.Clear();

            this.panel1.Controls.Clear();
        }
    }
}
